package com.w.wx.utils;

import com.github.binarywang.java.emoji.EmojiConverter;
import com.w.wx.domain.DTO.GoodsDeDTO;
import com.w.wx.domain.Goods;

import com.w.wx.domain.Msg;

import com.w.wx.domain.Notice;
import com.w.wx.domain.UserEr;
import com.w.wx.service.impls.ImagesServiceImpl;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

/**
 * 一个工具类，用于查询物品信息，将获取到的数据
 * 从ArrayList转移到HashMap
 */
@Component
@Slf4j
public class ALToHMUtil  {

    //解决无法注入imageServiceImpl==null异常
    public static ALToHMUtil alToHMUtil;
    @Autowired
    private ImagesServiceImpl imagesService;
    static EmojiConverter emojiConverter = EmojiConverter.getInstance();

    @PostConstruct        //此注解的方法在bean加载前执行
    private void init() {
        alToHMUtil = this;
        //初始化时将静态化的interFaceInfoMapper进行了实例化
        alToHMUtil.imagesService = this.imagesService;
    }

    /**
     * 封装详细信息，即物品详细信息页面
     * @return
     */
   public static  Msg goodsDetail(Goods good){
       if (good==null) {
           return Msg.fail();
       }
       int b=1;//定义图片名称
       HashMap<String,Object> mapGoods =new HashMap<>();
       HashMap<String,Object> mapImages = new HashMap<>();//将发布物品的图片加到物品详细信息

       Integer gid = good.getGid();

       ArrayList<String> imagesUrl = alToHMUtil.imagesService.getImagesByGid(gid);//获取该物品对应图片url

       Iterator<String> it = imagesUrl.iterator();
       while(it.hasNext()){//遍历将list中的数据转移到map
          mapImages.put("imageUrl"+b,it.next());
          b++;
       }
       good.setImageUrl(mapImages);
       mapGoods.put("goodInfo",good);//图片加到对应gid物品上


       Msg msg = new Msg();
       msg.setMsg("处理成功");
       msg.setCode(200);
       msg.setExtend(mapGoods);
       return msg;

   }

    public static  Msg goodsDetail(GoodsDeDTO good){
        if (good==null) {
            return Msg.fail();
        }
        int b=1;//定义图片名称
        HashMap<String,Object> mapGoods =new HashMap<>();
        HashMap<String,Object> mapImages = new HashMap<>();//将发布物品的图片加到物品详细信息

        Integer gid = good.getGid();

        ArrayList<String> imagesUrl = alToHMUtil.imagesService.getImagesByGid(gid);//获取该物品对应图片url

        Iterator<String> it = imagesUrl.iterator();
        while(it.hasNext()){//遍历将list中的数据转移到map
            mapImages.put("imageUrl"+b,it.next());
            b++;
        }
        good.setImageUrl(mapImages);
        mapGoods.put("goodInfo",good);//图片加到对应gid物品上


        Msg msg = new Msg();
        msg.setMsg("处理成功");
        msg.setCode(200);
        msg.setExtend(mapGoods);
        return msg;

    }

    /**
     * 封装简单信息，即封面
     * @param list
     * @return
     */
   public static Msg listTomap(ArrayList<Goods> list) {
       if (list.isEmpty()){
           return Msg.fail().add("tip","没有数据了");
       }
       HashMap<String,Object> mapGoods =new HashMap<>();
       int i=1;//定义商品名称
       Iterator<Goods> iterator = list.iterator();
       while(iterator.hasNext()) {

           mapGoods.put("goodsSimpleInfo"+i,iterator.next());
           i++;
       }
       Msg msg = new Msg();
       msg.setMsg("处理成功");
       msg.setCode(200);
       msg.setExtend(mapGoods);
       return msg;
    }
    public static Msg listTomapNotice(ArrayList<Notice> notice) {
        HashMap<String,Object> mapGoods =new HashMap<>();
        int i=1;//定义商品名称
        Iterator<Notice> iterator = notice.iterator();
        while(iterator.hasNext()) {

            mapGoods.put("Info"+i,iterator.next());
            i++;
        }
        Msg msg = new Msg();
        msg.setMsg("处理成功");
        msg.setCode(200);
        msg.setExtend(mapGoods);
        return msg;
    }

    public static Msg listTomapUserEr(ArrayList<UserEr> userErs) {
        HashMap<String,Object> mapGoods =new HashMap<>();
        int i=1;//定义商品名称
        Iterator<UserEr> iterator = userErs.iterator();
        while(iterator.hasNext()) {

            mapGoods.put("UserErInfo"+i,iterator.next());
            i++;
        }
        Msg msg = new Msg();
        msg.setMsg("处理成功");
        msg.setCode(200);
        msg.setExtend(mapGoods);
        return msg;
    }

    public static Msg retrunEasy(int b){
        if(b>=1){
            return Msg.success();
        }else{
            return Msg.fail();
        }
    }

    public static String toHtml(String content){

        return emojiConverter.toHtml(content);
    }

    public static String toUnicode(String content){
        return emojiConverter.toUnicode(content);
    }

}
